package cn.aib.staservice.service.impl;

import cn.aib.staservice.client.UcenterClient;
import cn.aib.staservice.entity.StatisticsDaily;
import cn.aib.staservice.mapper.StatisticsDailyMapper;
import cn.aib.staservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-26
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    
    @Override
    public Integer saveSta(String date) {

        //情况原有的旧统计数据
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();

        wrapper.eq("date_calculated", date);

        baseMapper.delete(wrapper);

        //保存新的统计数据
        Integer count = ucenterClient.getRegisterFrom(date);

        StatisticsDaily sta = new StatisticsDaily();

        sta.setRegisterNum(count);
        sta.setDateCalculated(date);//统计日期

        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));

        int i = baseMapper.insert(sta);

        return i;
    }

    @Override
    public Map<String, Object> showStaData(String type, String begin, String end) {

        //设置条件
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();

        wrapper.between("date_calculated", begin, end);

        wrapper.select("date_calculated",type);

        //查询
        List<StatisticsDaily> dailyList = baseMapper.selectList(wrapper);

        //遍历取值
        Map<String, Object> map = new HashMap<>();
        List<Integer> dataList = new ArrayList<Integer>();

        List<String> dateList = new ArrayList<String>();

        map.put("dataList", dataList);
        map.put("dateList", dateList);

        for (int i = 0; i < dailyList.size(); i++) {
            StatisticsDaily daily = dailyList.get(i);

            dateList.add(daily.getDateCalculated());

            switch (type) {
                case "register_num":
                    dataList.add(daily.getRegisterNum());
                    break;
                case "login_num":
                    dataList.add(daily.getLoginNum());
                    break;
                case "video_view_num":
                    dataList.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    dataList.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }

        return map;
    }
}
